package com.qfjy.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qfjy.client.VideoInfoFeignClient;
import com.qfjy.entity.po.MeetingPub;
import com.qfjy.service.MeetingPubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
@Slf4j
@RequestMapping("meetingPub")
@DefaultProperties(defaultFallback = "defaultFallback")
public class MeetingPubController {

    @Autowired
    private MeetingPubService meetingPubService;


    /**
     * TODO 根据会议编号查询会议信息  meetingPub/select?pcode=1111
     * @return
     */
    @ResponseBody
    @GetMapping("select")
    public Object selectMeetingPubByPcode(@RequestParam("pcode")String pcode){

       MeetingPub meetingPub= meetingPubService.selectMeetingPubByPcode(pcode);
       log.info("查询的是会议微服务信息：{}",meetingPub);


       //调用视频微服务 根据会议编号 得到视频相关信息
       // String url="http://localhost:8085/videoInfo/select?pcode="+meetingPub.getPcode();
        //Ribbon+RestTemplate
        // Ribbon可自动从Eureka Server获取服务提供者地址列表，并基于负载均衡算法，请求其中一个服务提供者实例。
      //  String url="http://QF-VIDEO/videoInfo/select?pcode="+meetingPub.getPcode();
      //  String result=restTemplate.getForObject(url,String.class);

        //Feign(Ribbon+RestTemplate)
       String result= videoInfoFeignClient.selectVideoInfoByPcode(meetingPub.getPcode());
        meetingPub.setRemark(result);

       //要给的是一个ResultJson 统一内容格式（统一内容协议）
        return meetingPub;
    }
    @Autowired
    private VideoInfoFeignClient videoInfoFeignClient;

    @Autowired
    private RestTemplate restTemplate;


    /**
     `服务降级：当服务内部出现异常情况，将触发降级，这个降级是每次请求都会去触发，走默认处理方法defaultFallback`
     `服务熔断：在一定周期内，服务异常次数达到设定的阈值或百分比，则触发熔断，熔断后，后面的请求将都走默认处理方法`
     */
    @GetMapping("info")
    @ResponseBody
    @HystrixCommand(fallbackMethod = "fallbackInfo")
    public String info(@RequestParam("id") int id){
        log.info("走这个info方法了吗---》");
        if(id>100){
            throw new RuntimeException("id值大于100");
        }

        return "正常请求并返回 info--->"+id;
    }

    public String fallbackInfo( int id){
        return "默认服务降级的默认值："+id;
    }

    @GetMapping("info3")
    @ResponseBody
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "6"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "5000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50")
    })
    public String info3(@RequestParam("id") int id){
        log.info("走这个info3方法了吗---》");
        if(id>100){
            throw new RuntimeException("id值大于100");
        }

        return "正常请求并返回 info3--->"+id;
    }




    @GetMapping("info1")
    @ResponseBody
    @HystrixCommand(fallbackMethod = "fallbackInfo1")
    public String info1(@RequestParam("id") int id){

        if(id>100){
            throw new RuntimeException("id值大于100");
        }

        return "正常请求并返回 info1--->"+id;
    }

    public String fallbackInfo1( int id){
        return "默认服务降级的默认值info1："+id;
    }

    @GetMapping("info2")
    @ResponseBody
    @HystrixCommand
    public String info2(@RequestParam("id") int id){

        if(id>100){
            throw new RuntimeException("id值大于100");
        }

        return "正常请求并返回 info2--->"+id;
    }

    //ResultJson
    public String  defaultFallback(){
        return "网络繁忙，稍后再试";
    }

}
